package com.linkstec.musiccloud.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;

import com.linkstec.musiccloud.pojo.Song;
import com.linkstec.musiccloud.pojo.User;
import com.linkstec.musiccloud.service.SongService;
import com.linkstec.musiccloud.service.SonglistService;
import com.linkstec.musiccloud.service.UserService;
import com.linkstec.musiccloud.util.Page;
import com.linkstec.musiccloud.util.SingerState;
import com.linkstec.musiccloud.vo.SongListVos;
import com.linkstec.musiccloud.vo.SongVo;
import com.sun.java_cup.internal.runtime.virtual_parse_stack;
import com.sun.org.apache.regexp.internal.recompile;

/**
 * @ClassName:ManagerController
 * @Description:TODO管理员的controller
 * @author liujinhui
 * @date:2018年11月23日上午9:43:28
 */
@Controller
@RequestMapping("manager")
public class ManagerController {
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private SongService songService;
	
	@Autowired
	private SonglistService songlistService;
	
	/**
	 * @Description:TODO跳转到管理员首页
	 * @author liujinhui
	 * @date:2018年11月23日上午11:38:28
	 * @return
	 */
	@RequestMapping(value="toMindex")
	public String toMindex(){
		return "mindex";
	}
	
	/**
	 * @Description:TODO跳转到用户管理页面
	 * @author liujinhui
	 * @date:2018年11月23日上午9:43:52
	 * @return
	 */
	@RequestMapping(value="toMUser")
	public ModelAndView toMUser(){
		ModelAndView mv = new ModelAndView();
		List<User> userlist = null;
		try {
			userlist = userService.selectAllUser();
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("userlist", userlist);
		mv.setViewName("muser");
		return mv;
	}
	
	/**
	 * @Description:TODO跳转到用户新增页面
	 * @author liujinhui
	 * @date:2018年11月23日上午9:48:01
	 * @return
	 */
	@RequestMapping(value="toMuserAdd")
	public String toMuserAdd(){
		return "museradd";
	}
	
	/**
	 * @Description:TODO添加用户
	 * @author liujinhui
	 * @date:2018年11月23日上午11:07:17
	 * @param user
	 * @return
	 */
	@RequestMapping(value="mUserAdd",method=RequestMethod.POST)
	public String mUserAdd(User user){
		try {
			userService.mUserAdd(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/manager/toMUser";
	}

	
	/**
	 * @Description:TODO查询用户信息并跳转到用户更新页面
	 * @author liujinhui
	 * @date:2018年11月23日下午3:04:05
	 * @param user
	 * @return
	 */
	@RequestMapping(value="toMuserUpdate")
	public ModelAndView toMuserUpdate(User user){
		ModelAndView mv = new ModelAndView();
		User user2 = null;
		try {
			user2 = userService.selectUserById(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("user", user2);
		mv.setViewName("muserupdate");
		return mv;
	}
	
	/**
	 * @Description:TODO修改用户信息
	 * @author liujinhui
	 * @date:2018年11月23日下午3:48:30
	 * @param user
	 * @return
	 */
	@RequestMapping(value="mUserUpdate")
	public String mUserUpdate(User user){
		try {
			userService.mUserUpdate(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/manager/toMUser";
	}
	
	/**
	 * @Description:TODO根据用户名模糊查询用户
	 * @author liujinhui
	 * @date:2018年11月23日下午4:09:11
	 * @param user
	 * @return
	 */
	@RequestMapping(value="selectUsers",method=RequestMethod.POST)
	public ModelAndView selectUsers(User user){
		ModelAndView mv = new ModelAndView();
		List<User> users = null;
		try {
			users = userService.selectUsers(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("userlist", users);
		mv.setViewName("muser");
		return mv;
	}
	
	/**
	 * @Description:TODO逻辑删除用户
	 * @author liujinhui
	 * @date:2018年11月23日下午5:02:20
	 * @param userId
	 * @return
	 */
	@RequestMapping(value="deleteUsers")
	public String deleteUsers(int[] userId){
		User user = new User();
		try {
			user.setIds(userId);
			userService.deleteUsers(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/manager/toMUser";
	}
	
	/**
	 * @Description:TODO查询所有歌曲信息并跳转到歌曲管理页面
	 * @author liujinhui
	 * @date:2018年11月24日下午3:00:19
	 * @return
	 */
	@RequestMapping(value="toMSong")
	public ModelAndView toMSong(Page page){
		ModelAndView mv = new ModelAndView();
		List<SongVo> songList = null;
		Page page2 = new Page();
		int counts = 0;
		try {
			
			//封装分页信息
			counts = songService.getAllSongs().size();
			page2.setRows(counts);
			page2.setCurPage(page.getCurPage());
			page2.setTotalPage((int) Math.ceil((double)counts/page2.getPageNumber()));
			
			songList = songService.selectAllSong(page);

		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("songList", songList);
		mv.addObject("page", page2);
		mv.setViewName("msong");
		return mv;
	}
	
	/**
	 * @Description:TODO跳转到歌曲添加界面
	 * @author liujinhui
	 * @date:2018年11月24日下午4:05:34
	 * @return
	 */
	@RequestMapping(value="toMsongAdd")
	public String toMsongAdd(){	
		return "msongadd";
	}
	
	/**
	 * @Description:TODO添加一首歌曲
	 * @author liujinhui
	 * @date:2018年11月24日下午4:14:24
	 * @param song
	 * @return
	 */
	@RequestMapping(value="mSongAdd",method=RequestMethod.POST)
	public String mSongAdd(Song song){
		try {
			songService.mSongAdd(song);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/manager/toMSong";
	}
	
	/**
	 * @Description:TODO删除歌曲
	 * @author liujinhui
	 * @date:2018年11月25日下午1:12:52
	 * @param songId
	 * @return
	 */
	@RequestMapping(value="deleteSongs")
	public String deleteSongs(Song song){
		try {
			songService.deleteSongs(song);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/manager/toMSong";
	}
	
	/**
	 * @Description:TODO查询歌曲信息并跳转到歌曲修改界面
	 * @author liujinhui
	 * @date:2018年11月25日下午2:42:59
	 * @param songId
	 * @return
	 */
	@RequestMapping(value="toMSongUpdate")
	public ModelAndView toMSongUpdate(int songId){
		ModelAndView mv = new ModelAndView();
		Song song = null;
		try {
			song = songService.selectSongById(songId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("song", song);
		mv.setViewName("msongupdate");
		return mv;
	}
	
	/**
	 * @Description:TODO修改歌曲信息
	 * @author liujinhui
	 * @date:2018年11月25日下午2:50:29
	 * @param song
	 * @return
	 */
	@RequestMapping(value="mSongUpdate")
	public String mSongUpdate(Song song){
		try {
			songService.mSongUpdate(song);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/manager/toMSong";
	}
	
	/**
	 * @Description:TODO根据查询选项返回不同的查询信息
	 * @author liujinhui
	 * @date:2018年11月25日下午4:47:05
	 * @param radio
	 * @param search
	 * @return
	 */
	@RequestMapping(value="selectSongs")
	public ModelAndView selectSongs(int radio,String search,Page page){
		ModelAndView mv = new ModelAndView();
		List<SongVo> songList = null;
		Page page2 = new Page();
		int counts = 0;
		try {
			//封装分页信息
			counts = songService.getAllSongs().size();
			page2.setRows(counts);
			page2.setCurPage(page.getCurPage());
			page2.setTotalPage((int) Math.ceil((double)counts/page2.getPageNumber()));
			//radio或search为空时，查询所有，否则如果radio为1，按歌手查询，如果为2，按歌曲名查询
			if(radio==0||(search==""||search==null)){
				songList = songService.selectAllSong(page);
			} else if(radio==1){
				songList = songService.selectSongsBySinger(search);
			} else if (radio==2) {
				songList = songService.selectSongsByName(search);
			} 
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("page", page2);
		mv.addObject("songList", songList);
		mv.setViewName("msong");
		return mv;
	}
	
	/**
	 * @Description:TODO查询要成为歌手的用户
	 * @author liujinhui
	 * @date:2018年11月25日下午5:24:36
	 * @return
	 */
	@RequestMapping(value="selectSingers")
	public ModelAndView selectSingers(){
		ModelAndView mv = new ModelAndView();
		List<User> userList = null;
		try {
			userList = userService.selectSingers();
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("userlist", userList);
		mv.setViewName("muser");
		return mv;
	}
	
	/**
	 * @Description:TODO
	 * @author liujinhui
	 * @date:2018年11月26日上午9:38:08
	 * @param user
	 * @return
	 */
	@RequestMapping(value="becomeSinger")
	public String becomeSinger(User user){
		try {
			userService.becomeSinger(user);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "redirect:/manager/toMUser";
	}
	
	/**
	 * @Description:TODO查找待审核的歌曲
	 * @author liujinhui
	 * @date:2018年11月26日上午11:53:22
	 * @return
	 */
	@RequestMapping(value="selectVatSongs")
	public ModelAndView selectVatSongs(Page page){
		ModelAndView mv = new ModelAndView();
		List<SongVo> songList = null;
		Page page2 = new Page();
		int counts = 0;
		try {
			songList = songService.selectVatSongs();
			counts = songList.size();
			page2.setRows(counts);
			page2.setCurPage(page.getCurPage());
			page2.setTotalPage((int) Math.ceil((double)counts/page2.getPageNumber()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("page", page2);
		mv.addObject("songList", songList);
		mv.setViewName("msong");
		return mv;
	}
	
	/**
	 * @Description:TODO审核通过歌曲
	 * @author liujinhui
	 * @date:2018年11月26日下午2:12:03
	 * @param ids
	 * @return
	 */
	@RequestMapping(value="submitSongs")
	public String submitSongs(int[] ids){
		try {
			songService.submitSongs(ids);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return  "redirect:/manager/toMSong";
	}
	
	
	/**
	 * @Description:TODO查询歌曲详情并跳转页面
	 * @author liujinhui
	 * @date:2018年11月27日下午2:05:02
	 * @return
	 */
	@RequestMapping(value="songDetail")
	public ModelAndView songDetail(SongVo song){
		ModelAndView mv = new ModelAndView();
		List<SongListVos> lists = null;
		try {
			lists = songlistService.selectListsBySongId(song.getSongId());
			song = songService.getSongVoById(song);
		} catch (Exception e) {
			e.printStackTrace();
		}
		mv.addObject("lists", lists);
		mv.addObject("song", song);
		mv.setViewName("songdetail");
		return mv;
	}

}
